// pages/game/list/index.js
import {typeWord, fetch, formatTime} from '../../../utils/index.js';

Page({

    /**
   * 页面的初始数据
   */
    data: {
        offset: 0,
        limit: 10,
        hasNextPage: true,
        games: []
    },

    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {},

    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},

    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        this.getGames(true);
    },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},

    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},

    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},

    getGames: function(refresh) {
        const that = this;
        let {offset, limit, hasNextPage, games} = that.data;
        if (refresh) {
            offset = 0;
            games = [];
        }
        fetch.post('game/get_games', {offset, limit}).then(function(res) {
            games = games.concat(res.list.map((game) => {
                game.create_time = formatTime(new Date(game.create_time * 1000));
                game.status = [
                    '已结束',
                    '游戏开始',
                    '时间线阐述',
                    '第一轮搜证',
                    '第一轮投票',
                    '第二轮搜证',
                    '第二轮投票',
                    '游戏结束'
                ][game.status];
                return game;
            }));
            offset = games.length;
            hasNextPage = offset < res.pages.totalCount;
            that.setData({games, hasNextPage, offset});
        });
    },
    toGameDetail: function(event) {
        console.log(event);
        wx.navigateTo({
            url: '/pages/game/play/index?id=' + event.currentTarget.dataset.game_id
        })
    }
})
